<?php
require_once 'classes/View.php';
require_once 'classes/DB.php';

session_start();

if (!isset($_SESSION['user']))
{
	header('Location: index.php');
}
else
{
	if (isset($_REQUEST['photo_id']))
	{
		/*View*/ $view = new View();
		/*Photo*/ $photo = new Photo(DB::getInstance());
		$photo->setPhotoId($_REQUEST['photo_id']);
		$photo->load();
		$_SESSION['photo'] = $photo;
		$view->display('photo');
	}
	else if (isset($_REQUEST['edit_btn']))
	{
		/*View*/ $view = new View();
		$_SESSION['photo']->setName($_REQUEST['name']);
		$_SESSION['photo']->setDescription($_REQUEST['description']);
		$error = $_SESSION['photo']->edit();
		if($error == "")
		{
			$view->data['notification'] = 'Edit successful';
		}
		else
		{
			$view->data['error'] = $error;
		}
		$view->display('photo');
	}
	else if (isset($_REQUEST['delete_btn']))
	{
		/*Category*/ $parentCategory = new Category(DB::getInstance());
		$parentCategory->setCategoryId($_SESSION['photo']->getCategoryId());
		$parentCategory->load();
		$_SESSION['photo']->delete();
		$view->data['notification'] = 'Delete successful';
		$_SESSION['category'] = $parentCategory;
		header('Location: category.php');
	}
	else if (isset ($_REQUEST['up']))
	{
		header('Location: category.php');
	}
	else
	{
		header('Location: category.php');
	}
}
?>